package edu.ua.cs.cs515.gcj.tetris;

import java.util.Random;

/**
 * The tetromino selection strategy is based on the current score that the user has reached
 * If the user has a score less than 1000 the strategy is to select tetrominos in the order
 * that they appear in our pre-shuffled list.  If the user has a score which is greater
 * than 1000 the strategy is to select a random tetromino.
 *
 */
public class TetrominoSelectionStrategy implements AbstractStrategy {
	
	// Our decision making boundary is at 1000 points
	private final int DECISION_BOUNDARY = 1000;
	
	// An array of possible tetromino types
	private char[] types = new char[]{'T','Z','S','I','O','J','L'};
	// The current selected index of the type array
	private int selection=0;
	// A random object for generating random numbers
	private Random random;
	
	// Constructor.  Creates the random object and then shuffles the array.
	public TetrominoSelectionStrategy() {
		random = new Random();
		shuffleTypes();
	}

	// Returns the next tetromino type 
	@Override
	public Character getNext(Object input) {
		Integer score = (Integer)input;
		if (score < DECISION_BOUNDARY) {
			return getTypeInOrder();
		} else {
			return getRandomType();
		}
	}
	
	// Shuffles the array of tetromino types
	public void shuffleTypes() {
		char temp;
		for (int i=0;i<types.length;i++) {
			int index = random.nextInt(types.length);
			temp = types[index];
			types[index] = types[i];
			types[i] = temp;
		}
	}
	
	// Returns the next tetromino type in order
	private Character getTypeInOrder() {
		if (selection>types.length-1) {
			selection = 0;
		}
		return types[selection++];
	}
	
	// Returns the next tetromino type randomly
	private Character getRandomType() {
		selection = random.nextInt(types.length);
		return types[selection];
	}

}
